<?php

namespace app\api\controller;

use app\api\BaseController;
use app\api\model\DepartmentModel;
use app\api\model\FileModel;
use app\api\model\FolderModel;
use app\api\model\ProcessModel;
use app\api\model\UserDepartmentModel;
use app\api\model\UserModel;
use think\facade\Log;

class DingIndex extends BaseController
{
    public function index()
    {
        $params = $this->request->param();

        $userInfo = $this->request->middleware('userInfo');

//        $userModel = UserModel::find($params['userId']);
        $userModel = UserModel::find($userInfo->id);

        $folderModel = new FolderModel();

//        if (empty($params['userId'])) {
//            return json('缺少重要参数userId',400);
//        }
        if (!empty($params['year']) && !empty($params['month'])) {
            $folderModel = $folderModel->whereMonth('finish_time', $params['year'].'-'.$params['month']);
        }

        if ($userModel['is_leader'] === 1) {
            $departmentIdList = UserDepartmentModel::where('user_id',$userModel['id'])
                ->column('department_id');

            if (empty($params['departmentId'])) {
                $map[] = ['department_id','in',$departmentIdList];
            }
        } else {
            $map = [
                [
                    'user_id','=',$userModel['id']
                ]
            ];
        }


        if (!empty($params['departmentId'])) {
            $map[] = ['department_id','=',$params['departmentId']];
        }

        if (!empty($params['processId'])) {
            $map[] = ['process_id','=',$params['processId']];
        }


        $modelList = $folderModel->with(['department','process','user'
//            'department' => function ($query) {$query->withField(['id','name']);},
//            'process' => function ($query) {$query->withField(['id','name']);}
        ])
            ->order('finish_time','desc')
            ->where($map)
            ->paginate($params['pageSize'],true);
//            ->toArray();

        $list = [];
        foreach ($modelList as $value) {
            $list[] = [
                'id' => $value['id'],
                'name' => $value['name'],
                'describe' => $value['describe'],
                'finishTime' => $value['finish_time'],
                'processId' => $value['process_id'],
                'department' => [
                    'id' => $value['department']['id'],
                    'name' => $value['department']['name']
                ],
                'process' => [
                    'id' => $value['process']['id'],
                    'name' => $value['process']['name']
                ],
                'user' => [
                    'id' => $value['user']['id'],
                    'name' => $value['user']['name'],
                ]
            ];
        }




        return json($list);
    }


    public function attachments()
    {
        $params = $this->request->param();

        if (empty($params['folderId'])) {
            return json('缺少重要参数folderId',400);
        }

        $list = FileModel::where('folder_id',$params['folderId'])->select();

        return json($list);

    }


    // 用于测试接口获取用户数据
    public function userList()
    {
        $list = UserModel::field('id,name')->select()->toArray();
        return json($list);
    }

    public function departmentList()
    {
        $userInfo = $this->request->middleware('userInfo');
        $params = $this->request->param();

        Log::write($userInfo,'myLog');

        $list = UserDepartmentModel::with(['department' => function ($query) {
            $query->withField(['id','name']);
        }])->where('user_id',$userInfo->id)->select()->toArray();
//        $list = UserModel::field('id,name')->select()->toArray();
        return json($list);
    }

    public function processList()
    {
        $params = $this->request->param();
        $list = ProcessModel::field(['id','name'])->select()->toArray();
        return json($list);
    }


}